package at.horario.web.component;

import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.feedback.FeedbackMessagesModel;
import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.feedback.IFeedbackMessageFilter;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import java.util.List;

public class FeedbackViewPanel extends Panel implements IFeedback {
    private static final long serialVersionUID = 1L;

    public FeedbackViewPanel(final String id) {
        super(id);
        final FeedbackMessagesModel errorMessagesModel = new FeedbackMessagesModel(this);
        errorMessagesModel.setFilter(new IFeedbackMessageFilter() {
            public boolean accept(FeedbackMessage message) {
                return message.getLevel() == FeedbackMessage.ERROR;
            }
        });
        final MessageListView errorMessagesRow = new MessageListView("errorMessagesRow", errorMessagesModel);
        final FeedbackMessagesModel successMessagesModel = new FeedbackMessagesModel(this);
        successMessagesModel.setFilter(new IFeedbackMessageFilter() {
            public boolean accept(FeedbackMessage message) {
                return message.getLevel() == FeedbackMessage.INFO;
            }
        });
        final MessageListView successMessagesRow = new MessageListView("successMessagesRow", successMessagesModel);

        WebMarkupContainer errorMessages = new WebMarkupContainer("errorMessages") {
            @Override
            public boolean isVisible() {
                return errorMessagesRow.isVisible();
            }
        };

        WebMarkupContainer successMessages = new WebMarkupContainer("successMessages") {
            @Override
            public boolean isVisible() {
                return successMessagesRow.isVisible();
            }
        };

        add(errorMessages.add(errorMessagesRow));
        add(successMessages.add(successMessagesRow));
    }

    private final class MessageListView extends ListView {
        private static final long serialVersionUID = 1L;

        public MessageListView(String id, final FeedbackMessagesModel model) {
            super(id, model);
            setVersioned(false);
        }

        public boolean isVisible() {
            final List messages = (List) getModelObject();
            return !messages.isEmpty();
        }

        public void populateItem(ListItem item) {
            // Access the item from the the FeedbackMessages list that
            // you supplied earlier.
            final FeedbackMessage message = (FeedbackMessage) item.getModelObject();
            message.markRendered();
            Label label = new Label("message", new PropertyModel(message, "message"));
            final Label m = new Label("icon");
            //item.add(m);
            m.setModel(new Model("<span class='" + (message.isError() ? "erro" : "mensagem") + "'/>"));
            m.setEscapeModelStrings(false);
            m.setRenderBodyOnly(true);


            item.add(label);
//            label.add(new AttributeModifier("class", true, new AbstractReadOnlyModel() {
//                public Object getObject() {
//                    return "";
//                    //return message.isError() ? "error" : "message";
//                }
//            }));
        }
    }
}
